#! /usr/bin/perl
#
#  TOPPERS Software
#      Toyohashi Open Platform for Embedded Real-Time Systems
# 
#  Copyright (C) 2006-2010 by Embedded and Real-Time Systems Laboratory
#              Graduate School of Information Science, Nagoya Univ., JAPAN
# 
#	The above copyright holders grant permission gratis to use,
#	duplicate, modify, or redistribute (hereafter called use) this
#	software (including the one made by modifying this software),
#	provided that the following four conditions (1) through (4) are
#	satisfied.
#
#	(1) When this software is used in the form of source code, the above
#    	copyright notice, this use conditions, and the disclaimer shown
#    	below must be retained in the source code without modification.
#
#	(2) When this software is redistributed in the forms usable for the
#    	development of other software, such as in library form, the above
#    	copyright notice, this use conditions, and the disclaimer shown
#    	below must be shown without modification in the document provided
#    	with the redistributed software, such as the user manual.
#
#	(3) When this software is redistributed in the forms unusable for the
#    	development of other software, such as the case when the software
#    	is embedded in a piece of equipment, either of the following two
#   	 conditions must be satisfied:
#
#		(a) The above copyright notice, this use conditions, and the
#         	disclaimer shown below must be shown without modification in
#     		the document provided with the redistributed software, such as
#      		the user manual.
#
# 		(b) How the software is to be redistributed must be reported to the
#     		TOPPERS Project according to the procedure described
#     		separately.
#
#	(4) The above copyright holders and the TOPPERS Project are exempt
#    	from responsibility for any type of damage directly or indirectly
#   	caused from the use of this software and are indemnified by any
#    	users or end users of this software from any and all causes of
#    	action whatsoever.
#
#	THIS SOFTWARE IS PROVIDED "AS IS." THE ABOVE COPYRIGHT HOLDERS AND
#	THE TOPPERS PROJECT DISCLAIM ANY EXPRESS OR IMPLIED WARRANTIES,
#	INCLUDING, BUT NOT LIMITED TO, ITS APPLICABILITY TO A PARTICULAR
#	PURPOSE. IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS AND THE
#	TOPPERS PROJECT BE LIABLE FOR ANY TYPE OF DAMAGE DIRECTLY OR
#	INDIRECTLY CAUSED FROM THE USE OF THIS SOFTWARE.
# 
#  @(#) $Id: makerelease 1866 2010-07-25 13:19:17Z ertl-hiro $
# 

require 'getcwd.pl';

$cwd = getcwd();
$cwd =~ s/^.*[\/\\]([^\/\\]+)$/$1/;
$prefix = "./".$cwd;

@file_list = ();
%file_list = ();

if ($#ARGV >= 0) {
	$arg1 = $ARGV[0];
	$arg1 =~ s/\.\///;
}
else {
	$arg1 = "MANIFEST";
}

sub gen_path
{
	local($base, $path) = @_;

	while ($path =~ /^..\/(.*)$/) {
		$path = $1;
		$base =~ s/(\/?)[^\/]*\/$/$1/;
	}
	return($base.$path);
}

sub read_file {
	local($filename) = @_;
	local($fh, $dirname);

	if ($filename =~ /^(.*\/)[^\/]*$/) {
		$dirname = $1;
	}
	else {
		$dirname = "";
	}

	open($fh, $filename) || die "Cannot open $infile";
	while ($line = <$fh>) {
		chomp $line;
		$line =~ s/\r$//;
		$line =~ s/[ \t]*\#.*$//;
		next if ($line =~ /^[ \t]*$/);

		if ($line =~ /^E_PACKAGE[ \t]+(.*)$/) {
			if ($package) {
				die "Duplicated E_PACKAGE directive.";
			}
			else {
				$package = $1;
				$e_package = 1;
			}
		}
		elsif ($line =~ /^PACKAGE[ \t]+(.*)$/) {
			if ($package) {
				if (!$e_package && $package != $1) {
					die "Inconsistent PACKAGE directive.";
				}
			}
			else {
				$package = $1;
			}
		}
		elsif ($line =~ /^VERSION[ \t]+(.*)$/) {
			if ($version) {
				if (!$e_package && $version != $1) {
					die "Inconsistent VERSION directive.";
				}
			}
			else {
				$version = $1;
				if ($version =~ /%date/) {
					@current_time = localtime();
					$vdate = sprintf("%04d%02d%02d", $current_time[5]+1900,
										$current_time[4]+1, $current_time[3]);
					$version =~ s/%date/$vdate/;
				}
			}
		}
		elsif ($line =~ /^INCLUDE[ \t]+(.*)$/) {
			do read_file(do gen_path($dirname, $1));
		}
		else {
			$filename = $prefix."/".$dirname.$line;
			while ($filename =~ s|/[^/]+/\.\./|/|) {}
			if ($file_list{$filename}) {
				die "$filename is duplicated.";
			}
			else {
				push(@file_list, $filename);
				$file_list{$filename} = $filename;
			}
		}
	}
	close($fh);
}

do read_file($arg1);
unless ($package) {
	die "PACKAGE/E_PACKAGE directive not found.";
}
unless ($version) {
	die "VERSION directive not found.";
}

if (! -d "RELEASE") {
	mkdir("RELEASE");
}

$archive_name = $package."-".$version.".tar.gz";
$file_list = join(" ", @file_list);
$command = "tar cvfz RELEASE/$archive_name -C .. $file_list\n";
die if (system($command));
print STDERR "== RELEASE/$archive_name is generated. ==\n";
